package com.itheima.grow.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

/**
 * 定制化Excel模板导出工具类
 */
@Slf4j
public class CustomExcelUtil {

    private static final String TEMPLATED_PREFIX = "templates/";

    /**
     * 自定义模板生成Excel
     * @param templateName
     * @param paras
     * @return
     */
    public static Workbook generateExcel(String templateName, Object paras) throws RuntimeException, IOException {
        InputStream resourceAsStream = CustomExcelUtil.class.getClassLoader().getResourceAsStream("templates/" + "SFT工单导出模板.xlsx");
//        ClassPathResource resource = new ClassPathResource("\\templates\\SFT工单导出模板.xlsx");
//        InputStream resourceAsStream = resource.getInputStream();
        XLSTransformer transformer = new XLSTransformer();
        Workbook workbook = null;
        try (OutputStream out = new ByteArrayOutputStream()) {
            workbook = transformer.transformXLS(resourceAsStream, JSONObject.parseObject(JSONObject.toJSONString(paras), Map.class));
            return workbook;
        } catch (Exception e) {
            log.info("生成Excel异常信息:{}", e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 伪造数据
     * @return
     */
    public static Map<String, Object> mockData() {
        Map<String, Object> param = new HashMap<>();
        param.put("orderCode", "DWO2023120901");
        param.put("orderType", "答疑");
        param.put("oneClassify", "财务问题");
        param.put("twoClassify", "返利贴息");
        param.put("orderStatus", "已完成");
        param.put("oneClassify", "财务问题");
        param.put("twoClassify", "返利贴息");
        param.put("orderStatus", "已完成");

        List<Map> list = new ArrayList<>();
        Map<String, Object> param1 = new HashMap<>();
        param1.put("index", 1);
        param1.put("operateTime", DateUtil.formatDate(new Date(), DateUtil.FORMAT_6));
        param1.put("operatorType", "追问");
        list.add(param1);
        Map<String, Object> param2 = new HashMap<>();
        param2.put("index", 2);
        param2.put("operateTime", DateUtil.formatDate(new Date(), DateUtil.FORMAT_6));
        param2.put("operatorType", "回复");
        list.add(param2);
        param.put("items", list);
        return param;
    }

}
